package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.search.CustomSearchRecord;
import cn.jinjimi.app.search.Record;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * Created by Roy
 * Date: 15/12/4
 */
public class CustomSearchAdapter extends MyBaseAdapter<Record> {

    private Context mContext;

    public CustomSearchAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        CustomSearchRecord item = (CustomSearchRecord) getItem(position);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.search_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SpannableString spanString = new SpannableString(item.custom.name);
        ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.orange));
        spanString.setSpan(span, item.index, item.index + item.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.tvTitle.setText(spanString);

        return convertView;
    }

    static class ViewHolder {
        TextView tvTitle;

        public ViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_name);
        }
    }
}
